﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BankSystemWithCSharp.UserPanel.FuncSubpanel
{
    public partial class RecordPanel : UserControl
    {
        private IRecord recordController = new RecordController();
        private int sortColumn = 0;
        bool reserve = false;
        public RecordPanel()
        {
            InitializeComponent();
            listView1.ColumnClick += listView1_ColumnClick;
        }
        public void RefreshRecord()
        {
            sortColumn = 0;
            reserve = false;
            ShowRecord(TransformRecordArbiName.None);
        }
        private void ShowRecord(TransformRecordArbiName orderBy, bool reverse = false)
        {
            listView1.Items.Clear();
            var records = recordController.GetRecord(orderBy);
            if (reverse)
                records = records.Reverse();
            foreach (var record in records)
            {
                ListViewItem item = new ListViewItem(record.ID);
                item.Text = record.ID;
                ListViewItem.ListViewSubItem timeItem = new ListViewItem.ListViewSubItem(item, record.Time.ToString());
                item.SubItems.Add(timeItem);
                ListViewItem.ListViewSubItem moneyFromItem = new ListViewItem.ListViewSubItem(item, record.MoneyFrom);
                item.SubItems.Add(moneyFromItem);
                ListViewItem.ListViewSubItem moneyToItem = new ListViewItem.ListViewSubItem(item, record.MoneyTo);
                item.SubItems.Add(moneyToItem);
                ListViewItem.ListViewSubItem moneyItem = new ListViewItem.ListViewSubItem(item, record.Money.ToString("0.00"));
                item.SubItems.Add(moneyItem);
                listView1.Items.Add(item);
            }
        }
        private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (sortColumn == e.Column)
            {
                reserve = !reserve;
            } 
            else
            {
                sortColumn = e.Column;
                reserve = false;
            }
            TransformRecordArbiName orderBy = (TransformRecordArbiName)(sortColumn + 1);
            ShowRecord(orderBy, reserve);
        }
    }
}
